﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Helpers;
using System.Web.Mvc;
using DocumentFormat.OpenXml.Math;
using JinfuyuanProject.Entity.Filter;
using JinfuyuanProject.Service;
using JinfuyuanProject.Service.Interface;

namespace JinfuyuanProject.Controllers
{
    /*
     *赵莹
     *套餐内/外服务列表
     *2025-4-29
     */

    public class PackageSpendController : Controller
    {
        private readonly IPackageServicesService _packageServicesService;

        // GET: PackageSpend

        public PackageSpendController(IPackageServicesService packageServicesService)
        {
            _packageServicesService = packageServicesService;
        }
        #region 展示页面
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult PackageService(int Id)
        {
            ViewBag.Id = Id;
            return View();
        }
        #endregion

        #region 接口
        //套餐内消费
        public JsonResult PackagesInDetail(int page, int limit,int ContractId)
        {
            PackageSpendFilter filter = new PackageSpendFilter();
            filter.page = page;
            filter.limit = limit;
            filter.Id = ContractId;
            return Json(_packageServicesService.QueryInBound(filter), JsonRequestBehavior.AllowGet);
        }
        //套餐外消费
        public JsonResult PackagesOutDetail(int page, int limit, int ContractId)
        {
            PackageSpendFilter filter = new PackageSpendFilter();
            filter.page = page;
            filter.limit = limit;
            filter.Id = ContractId;
            return Json(_packageServicesService.QueryOutBound(filter), JsonRequestBehavior.AllowGet);
        }
        //清单列表数据
        public JsonResult InOutDetail(int page, int limit, int ContractId)
        {
            PackageSpendFilter filter = new PackageSpendFilter();
            filter.page = page;
            filter.limit = limit;
            filter.Id = ContractId;
            return Json(_packageServicesService.QueryList(filter), JsonRequestBehavior.AllowGet);
        }
        public FileResult ServiceDetail(int ContractId)
        {
            PackageSpendFilter filter = new PackageSpendFilter();
            filter.Id =ContractId;
            var result = _packageServicesService.GenerateExcel(filter);
            // 3. 读取文件并返回下载
            string filePath = result["filePath"]?.ToString();
            string fileName= result["fileName"]?.ToString();
            byte[] fileBytes = System.IO.File.ReadAllBytes(filePath);
            return File(
                fileBytes,
                "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                $"{fileName}"
            );
        }
        public JsonResult CheckOutQuit(int contractId)
        {
            return Json(_packageServicesService.CheckOut(contractId), JsonRequestBehavior.AllowGet);
        }

        #endregion

    }
}